package ZhengZe;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

/**
 * 文件加密
 * 
 * @author AcerMr
 *
 */
public class FileEncrypt
{
	/* 桶的大小 */
	static int tongSize = 100;
	/* 桶从什么时候开始有东西 */
	static int tongSt = 20;
	/* 桶从下标为20开始装，装30个 */
	static int tongLen = 50;

	public static void main(String[] args)
	{
		File souFile = new File("D:/a/1.jpg");
		File encryFile = new File("D:/a/1_a.jpg");
		File decryFile = new File("D:/a/1_b.jpg");
		encryFile(souFile, encryFile);
		decryFile(encryFile, decryFile);
		System.out.println("");
	}

	private static void decryFile(File a, File b)
	{
		long at = System.currentTimeMillis();
		BufferedInputStream fos = null;
		BufferedOutputStream fis = null;
		int count = 0;
		try
		{
			FileInputStream fileinputstream = new FileInputStream(a);
			FileOutputStream fileoutputstream = new FileOutputStream(b);
			fis = new BufferedOutputStream(fileoutputstream);
			fos = new BufferedInputStream(fileinputstream);
			byte[] rongliang = new byte[tongSize];
			int len = 0;
			while ((len = fos.read(rongliang)) != -1)
			{
				System.out.println(count + "==>" + len + "===>" + Arrays.toString(rongliang));
				if (len == tongSize)
				{
					fis.write(rongliang, tongSt, tongLen);
				} else
				{
					fis.write(rongliang, 0, len);
				}
				count++;
			}
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if (fos != null)
				{
					fos.close();
					fos = null;
				}
			} catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try
			{
				if (fis != null)
				{
					fis.close();
					fis = null;
				}
			} catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		long da = System.currentTimeMillis();
		System.out.println(da - at);
	}

	private static void encryFile(File a, File b)
	{
		long at = System.currentTimeMillis();
		BufferedInputStream fos = null;
		BufferedOutputStream fis = null;
		int count = 0;
		try
		{
			FileInputStream fileinputstream = new FileInputStream(a);
			FileOutputStream fileoutputstream = new FileOutputStream(b);
			fis = new BufferedOutputStream(fileoutputstream);
			fos = new BufferedInputStream(fileinputstream);
			byte[] rongliang = new byte[tongSize];
			Random random = new Random();
			int len = 0;
			while ((len = fos.read(rongliang, tongSt, tongLen)) != -1)
			{
				/*文件前面的干扰信息*/
				for(int i = 0 ; i < tongSt ; i++) 
				{
					byte temps = (byte)(random.nextInt(256)-128);
					rongliang[i] = temps;
				}/*文件后面的干扰信息*/
				for (int i = tongSt + tongLen+1; i < rongliang.length; i++)
				{
					byte temps = (byte)(random.nextInt(256)-128);
					rongliang[i] = temps;
				}
				System.out.println(count + "==>" + len + "===>" + Arrays.toString(rongliang));
				if (len == tongLen)
				{
					fis.write(rongliang);
				} else
				{
					fis.write(rongliang, tongSt, len);
				}
				count++;
			}
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			try
			{
				if (fos != null)
				{
					fos.close();
					fos = null;
				}
			} catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try
			{
				if (fis != null)
				{
					fis.close();
					fis = null;
				}
			} catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		long da = System.currentTimeMillis();
		System.out.println(da - at);

	}
}
